<table>
<tr><?php if($is_cancelable) {?><a style="width: 68px;" class="btn btn-primary" onclick="choose_room()">Add Room</a><br><br><?php } ?></tr>
</table>
<form action="<?php echo base_url().'reservation/addRoom/'?>" method="post">
<table class="table table table-bordered table-hover table-condensed">
    <thead>
        <tr>
            <th>Description</th><th style="display:none"></th style="display:none"><th style="display:none"></th><th>Room</th><th>Room Type</th><th>Rate Type</th><th style="display:none"></th><th>Reservation Date</th><th>Status</th><th>Booking Time</th><th>Check In Time</th><th>Check Out Time</th><th>Price</th><th colspan=3>Action</th>
        </tr>
    </thead>
	<tbody id="new_Room">
    <?php
	$id = 0;
	foreach ($data as $row){ 
		$id++; 
		if($row['deleted']=='No'){ 
		echo '<tr>'; 
		echo '<td>Last Ordered Room</td><td style="display:none"><input id="resv_room_id-'.$id.'" name="resv_room_id-'.$id.'" value="'.$row['resv_room_id'].'" style="display:none"></td>'.'<td style="display:none"><input id="resv_id" name="resv_id[]" value="'.$row['resv_id'].'" style="display:none"></td>'; 
		echo '<td>'.$row['room_number'].'</td>'.'<td>'.$row['room_type_name'].'</td>'.'<td>'.$row['rate_type_name'].'</td>'. '<td style="display:none"><input id="room_id-'.$id.'" name="room_id-'.$id.'" value="'.$row['room_id'].'" style="display:none"></td>'.
				'<td>'.$row['resv_room_date'].'</td>'.'<td>'.$row['resv_status'].'</td>'.'<td>'.$row['booking_time'].'</td>'.'<td>'.$row['check_in_time'].'</td>'. 
				'<td>'.$row['check_out_time'].'</td>'.'<td>'.'Rp '.number_format($row['price'], 2, ',', '.').'</td>'. 
				'<td>'; if($row['resv_status']=='Booked'){ if($row['resv_room_date'] > $row['now']) echo '<a type="submit" class="btn btn-primary btn-mini" title="Check In Room" disabled>Check In</a>&nbsp;'; else echo '<a type="submit" class="btn btn-primary btn-mini" title="Check In Room" onclick="check_in_last_room('.$id.')">Check In</a>&nbsp;'; echo '<a type="submit" class="btn btn-warning btn-mini" title="Cancel Room" onclick="cancel_last_room('.$id.')">Cancel</a>&nbsp;'; } else { if($row['resv_status']=='Check In'){ echo '<a type="submit" class="btn btn-success btn-mini" title="Check Out Room" onclick="check_out_last_room('.$id.')">Check Out</a>&nbsp;'; } else { echo ''; } } echo '<a type="submit" href="'.base_url('reservation/room_service/'.$row['resv_id'].'/'.$row['resv_room_id']).'" class="btn btn-success btn-mini" title="Room Services">Room Services</a>'; echo '</td>'; echo '</tr>'; 
				} 
	}


    /*foreach ($data as $row){
		$id++;
		if($row['deleted']=='No'){
        echo '<tr>';
        echo '<td>Last Ordered Room</td><td style="display:none"><input id="resv_room_id-'.$id.'" name="resv_room_id-'.$id.'" value="'.$row['resv_room_id'].'" style="display:none"></td>'.'<td style="display:none"><input id="resv_id" name="resv_id[]" value="'.$row['resv_id'].'" style="display:none"></td>';
        echo '<td>'.$row['room_number'].'</td>'.'<td>'.$row['room_type_name'].'</td>'.'<td>'.$row['rate_type_name'].'</td>'.
				'<td>'.$row['resv_room_date'].'</td>'.'<td>'.$row['resv_status'].'</td>'.'<td>'.$row['booking_time'].'</td>'.'<td>'.$row['check_in_time'].'</td>'.
                '<td>'.$row['check_out_time'].'</td>'.'<td>'.'Rp '.number_format($row['price'], 2, ',', '.').'</td>'.
				'<td>';
		if($row['resv_status']=='Booked'){
        echo '<a type="submit" class="btn btn-primary" title="Check In Room" onclick="check_in_last_room('.$id.')">Check In</a>'.
				'<a type="submit"  class="btn btn-success" title="Check Out Room" onclick="check_out_last_room('.$id.')">Check out</a>'.
				'<a type="submit" class="btn btn-warning" title="Cancel Room" onclick="cancel_last_room('.$id.')">Cancel</a>';
		} else {
			if($row['resv_status']=='Check In'){
				echo '<a type="submit"  class="btn btn-success" title="Check Out Room" onclick="check_out_last_room('.$id.')">Check Out</a>';
			} else {
				echo '';
			}
		}
		echo '<a type="submit" href="'.base_url('reservation/room_service/'.$row['resv_id'].'/'.$row['resv_room_id']).'" class="btn btn-success" title="Room Services">Room Services</a>';
		echo '</td>';
		 echo '</tr>';
	}
}*/
    ?>
	</tbody>
</table>
<a href="<?= base_url('reservation') ?>" class="btn">Back to Reservation</a>
<br><br>
	<table class="table table-bordered" id="tbl-addroom" style="display:none;">
		<tr>
			<th style="vertical-align:middle;">Room Ordered List</th>
			<td>
			<table class="table table-bordered">
				<thead>
					<th>Reservation Status</th>
					<th>Date</th>
					<th style="display:none;"></th>
					<th style="display:none;"></th>
					<th>Room</th>
					<th style="display:none;"></th>
					<th>Room Type</th>
					<th>Rate Type</th>
					<th>Room Price</th>
				</thead>
				<tbody id="ord_Room">
				
				</tbody>
			</table>
			
			</td>
		</tr>
		
	</table>
	<input id="btn-save" style="display:none;" type="submit" value="Save" onclick="post();" class="btn btn-primary">
</form>

<script>
var i = 0;
var room_ord = 0;
var room = 0;
var rp = {aSep: '.',aDec: ',',vMin: '0.00',vMax: '999999999999.99'};
    $(document).ready(function() {
        $('#datepicker').datetimepicker({
		  pickTime: false
		});
		
		$('#adult,#child,#infant').autoNumeric('init', {aSep: '',aDec: ',',vMin: '0',vMax: '999'});
    });

	
/*function list_room(resv_id, tanggal, room_type){
		$.post('<?php echo base_url(); ?>reservation/orderNewRoom/', {
			resv_id: $('#resv_id').val(),
            tanggal: $('#tanggal').val(),
			status: $('#statusord').val(),
            room_id: $('#room_id_ord').val(),
            room_numb: $('#room_numb_ord').text(),
            type: $('#room_type_ord').text(),
            price: $('#room_price_ord').text()
        }, function(data){
			room_ord++;
			//var ordroom = '<tr><td>New Ordered Room List</td><td></td><td></td><td>Room Number</td><td>Room Type</td><td>Rate Type</td><td>Reservation Date</td><td>Status</td><td>Booking Time</td><td>Check In Time</td><td>Check Out Time</td><td>Price</td><th colspan=3>Action</td></tr>';
				var ordroom = '<tr id="new_room-'+room_ord+'"><td>New Ordered Room</td>';
				ordroom += '<td ><input id="new_resv_room_id-'+room_ord+'" name="new_resv_room_id-'+room_ord+'" value=" '+data['resv_room_id']+'"></td>';
				ordroom += '<td><input name="resv_id" value=" '+data['resv_id']+' "></td>';
				ordroom += '<td>'+data['room_number']+'</td><td>'+data['room_type_name']+'</td><td>'+data['rate_type_name']+'</td>';
				ordroom += '<td>'+data['resv_room_date']+'</td><td>'+data['resv_status']+'</td><td>'+data['booking_time']+'</td><td>'+data['check_in_time']+'</td>';
                ordroom += '<td>'+data['check_out_time']+'</td><td class="rp">'+'Rp '+data['price']+'</td>';
				ordroom += '<td><a style="width: 55px;" class="btn btn-primary" title="Check In Room" onclick="check_in('+room_ord+'")">Check In</td>';
				ordroom += '<td><a style="width: 67px;" class="btn btn-success" title="Check Out Room" onclick="check_out('+room_ord+')">Check Out</td>';
				ordroom += '<td><a class="btn btn-warning" title="Cancel Room" onclick="cancel('+room_ord+')");">Cancel</td>';
				ordroom += '</tr>';
				
			
			//alert(ordroom);
			$('#new_Room').append(ordroom);
			alert(data['price']);
			$('.rp').autoNumeric('init',rp);
			$('#ord_Room').html('');
				$('#ord_Room').append('<tr id="listRoomOrd">');
				$('#ord_Room').append('<td width="50" id="resv_id" name="resv_id">'+data[key].resv_room_id+'</td>');
				$('#ord_Room').append('<td width="50" id="resv_status" name="resv_status">'+data[key].resv_status+'</td>');
				$('#ord_Room').append('<td width="50" name="room_numb" id="room_numb">' + data[key].room_number + '</td>');
				$('#ord_Room').append('<td width="50" id="room_type" name="room_type">'+room_type+'</td>');
				$('#ord_Room').append('<td width="50" id="room_price" name="room_price" >'+data[key].price+'</td>');
				$('#ord_Room').append('</tr>');
				
				$('#modalSearchRoom').modal('hide');
				
			},'json');
	}*/
	
	
	
	function cancel(id){
		$.post('<?php echo base_url(); ?>reservation/cancelRoomOrdered/', {
            resv_id: $('#resv_room_id-'+id).val()
        });
		location.reload(true);
		
	}
	
	function cancel_last_room(id){
		/*$.post('<?php echo base_url(); ?>reservation/cancelRoomOrdered/', {
            resv_id: $('#resv_room_id-'+id).val()
        });
		location.reload(true);*/
		var c = confirm('Cancel this room?');
		if(!c) return false;
		window.location = '<?= base_url('reservation/cancelRoomOrdered/'.$resv_id) ?>/'+$('#resv_room_id-'+id).val();
	}
	
	function check_in_last_room(id){
		/*$.post('<?php echo base_url(); ?>reservation/checkinRoom/', {
            resv_id: $('#resv_room_id-'+id).val()
        });
		location.reload(true);*/
		var c = confirm('Check In this room?');
		if(!c) return false;
		window.location = '<?= base_url('reservation/checkinRoom/'.$resv_id) ?>/'+$('#resv_room_id-'+id).val();
	}
	
	function check_in(id){
		$.post('<?php echo base_url(); ?>reservation/checkinRoom/', {
            resv_id: $('#new_resv_room_id-'+id).val()
        });
		location.reload(true);
	}

	function check_out_last_room(id){
		/*$.post('<?php echo base_url(); ?>reservation/checkoutRoom/', {
            resv_id: $('#resv_room_id-'+id).val(), 
			room_id: $('#room_id-'+id).val()
        });
		location.reload(true);*/
		var c = confirm('Check Out this room?');
		if(!c) return false;
		window.location = '<?= base_url('reservation/checkoutRoom/'.$resv_id) ?>/'+$('#resv_room_id-'+id).val()+'/'+$('#room_id-'+id).val();
	}
	
	function check_out(id){
		$.post('<?php echo base_url(); ?>reservation/checkoutRoom/', {
            resv_id: $('#new_resv_room_id-'+id).val()
        });
		location.reload(true);
	}
	
	function choose_room(){
		$('#tbl-addroom').css('display', 'inherit');
		$('#btn-save').css('display', 'inherit');
		window.open('<?php echo base_url();?>reservation/choose_room/','', 'toolbar=no,location=no,menubar=no,scrollbars=yes,width='+(screen.width-10)+',height='+(screen.height-100));
	}
	
	function choose_rooms(room_id, date) {
		
			//alert($(this).children('[name="room_id"]').val());
			//alert($(this).children('[name="date"]').val());
			
			$.post('<?php echo base_url(); ?>reservation/choose_room_model/',{
				room_id: room_id,
				date: date
			}, 
			function(data){
            $.each(data['room'], function(key, value) {
				room++;
				var room_t_id = data['room'][key].room_type_id;
                var room_avail = '<tr id="room-'+room+'" >';
					room_avail += '<td><select name="statusord[]" id="statusord-'+room+'" style="width:120px;"><option value="Booked">Booking</option><option value="Check In">Check In</option></select></td>';
					room_avail += '<td><input type="text" name="date[]" id="date-'+room+'" value="'+data['room'][key].resv_room_date+'" style="width:80px;" disabled></td>';
					room_avail += '<td style="display:none;"><input id="new_resv_room_id-'+room+'" name="new_resv_room_id[]" class="resv" value=" '+data['room'][key].resv_room_id+' " style="display:none"></td>';
					room_avail += '<td style="display:none;"><input id="room_id_ord-'+room+'" name="room_id_ord[]" value=" '+data['room'][key].room_id+' " style="display:none"></td>';
					room_avail += '<td ><input type="text" name="room_numb_ord[]" id="room_numb_ord-'+room+'" value="'+data['room'][key].room_number+'" disabled ></td>';
					room_avail += '<td style="display:none;"><input name="room_type_id[]" id="room_type_id-'+room+'" value="'+room_t_id+'" style="display:none"></td>';
					room_avail += '<td name="room_type_ord[]" id="room_type_ord-'+room+'">' + data['room'][key].room_type_name + '</td>';
					//room_avail += '<td name="room_price_ord[]" id="room_price_ord-'+room+'">' + data['room'][key].room_type_price + '</td>';
					room_avail += '<td><input type="hidden" name="room_price_ord[]" id="room_price_ord-'+room+'" value="'+data['room'][key].room_type_price+'"><select name="rate[]" id="rate-'+room+'" onchange="select_rate_price('+room+')"><option value="0">--Standard Rate--</option>';
				$.each(data['rate'], function(key, value) {
					//var rate_t_id = data['rate'][key].room_type_id;
					
					//if(room_t_id == rate_t_id) {
					
					room_avail += '<option value="'+data['rate'][key].rate_type_id+'">'+data['rate'][key].rate_type_name+'</option>';
					
					//$('#roomAvailable').append('<td name="rate_id'+rate+'" id="rate_id'+rate+'" style="display:none">' +data['rate'][key].rate_type_id + '</td>'); 
					//$('#roomAvailable').append('<td name="rate_id" id="rate_id">' +data['rate'][key].room_type_id + '</td>'); 
					//$('#roomAvailable').append('<td name="rate_name'+rate+'" id="rate_name'+rate+'">' +data['rate'][key].rate_type_name + '</td>'); 
					
				//	} else {
					//room_avail +='<option disabled></option>';
					//}
				});
				room_avail += '</select></td>';
				room_avail += '<td name="rate_price" id="rate_price-'+room+'"><input type="text" name="rate_price2[]" value="'+data['room'][key].room_type_price+'" disabled style="width:150px;"></td>';
				room_avail += '<td><a class="btn btn-warning" title="cancel room" onclick="cancel_new_room('+room+')");"><i class="icon-remove icon-white"></i></a></td>';
                room_avail += '</tr>';
				//alert(room_avail);
				$('#ord_Room').append(room_avail);
            });
			
				
        }, 'json');
		
		
   }
   
   function select_rate_price(row){
	$('#rate_price-'+row).empty();
	if($('#rate-'+row).val() == 0){
		var val = '<input type="text" name="rate_price2[]" value="'+$('#room_price_ord-'+row).val()+'" disabled style="width:150px;">';
					$('#rate_price-'+row).append(val);
	} else {
		$.post('<?php echo base_url(); ?>reservation/select_rate/',{		
				rate_id: $('#rate-'+row).val(),
				date: $('#date-'+row).val(),
				room_type_id: $('#room_type_id-'+row).val()
			}, function(data){
					//alert($('#room_price_ord-'+row).text());
					/*if(data.length==0){
					var val = '<input value="'+$('#room_price_ord-'+row).text()+'">';
					$('#rate_price-'+row).append(val);
					} else{*/
					var val = '<input type="text" name="rate_price2[]" value="'+data['room_rate_price']+'" disabled style="width:150px;">';
					$('#rate_price-'+row).append(val);
					//}
				}, 'json');
		}
   }
   
   function cancel_new_room(row){
		$.post('<?php echo base_url(); ?>reservation/cancelRoom/', {
            resv_id: $('#new_resv_room_id-'+row).val()
        });
		$('#room-'+row).remove();
	}
	
	function post(e) {
	if($('#ord_Room tr').length == 0){
		alert('Please choose room first.');
		event.preventDefault();
		return false;
	}
	
	var a=window.confirm('Are you sure that you want to proccess this reservation?');
	if(!a) {
		event.preventDefault();
		return false;
	} else {
		$('input').removeAttr('disabled');
	}
	
}
</script>